package view;

import ds.SkillTree;
import ds.MyTree;
import java.awt.*;

public class SkillTreeView {
    private SkillTree skillTree;
    public SkillTreeView(SkillTree skillTree) {
        this.skillTree = skillTree;
    }
    public void draw(Graphics g, int panelWidth, int mapWidth) {
        int rightPanelWidth = panelWidth - mapWidth;
        int nodeSize = 40;
        int minGap = 24;
        MyTree.Node root = skillTree.getTree().getRoot();
        int treeWidth = getSubtreeWidth(root, nodeSize, minGap);
        int startX = mapWidth + (rightPanelWidth - treeWidth) / 2;
        int startY = 200;
        g.setFont(new Font("微软雅黑", Font.BOLD, 18));
        g.setColor(Color.WHITE);
        g.drawString("技能树", startX + treeWidth/2 - 30, startY - 40);
        drawSkillTreeNodeSmart((Graphics2D)g, root, startX + treeWidth/2, startY, nodeSize, minGap);
        // 你可以继续迁移技能列表和叶子节点的显示逻辑
    }
    private int getSubtreeWidth(MyTree.Node node, int nodeSize, int minGap) {
        if (node == null) return 0;
        if (node.left == null && node.right == null) return nodeSize;
        int leftWidth = getSubtreeWidth(node.left, nodeSize, minGap);
        int rightWidth = getSubtreeWidth(node.right, nodeSize, minGap);
        if (leftWidth == 0) leftWidth = nodeSize;
        if (rightWidth == 0) rightWidth = nodeSize;
        return leftWidth + rightWidth + minGap;
    }
    private void drawSkillTreeNodeSmart(Graphics2D g, MyTree.Node node, int x, int y, int nodeSize, int minGap) {
        if (node == null) return;
        int leftWidth = getSubtreeWidth(node.left, nodeSize, minGap);
        int rightWidth = getSubtreeWidth(node.right, nodeSize, minGap);
        // 画连线和递归画子节点
        if (node.left != null) {
            int childX = x - (rightWidth + minGap) / 2;
            g.setColor(new java.awt.Color(120,255,180));
            g.setStroke(new java.awt.BasicStroke(3));
            g.drawLine(x + nodeSize/2, y + nodeSize, childX + nodeSize/2, y + 60);
            drawSkillTreeNodeSmart(g, node.left, childX, y + 60, nodeSize, minGap);
        }
        if (node.right != null) {
            int childX = x + (leftWidth + minGap) / 2;
            g.setColor(new java.awt.Color(120,255,180));
            g.setStroke(new java.awt.BasicStroke(3));
            g.drawLine(x + nodeSize/2, y + nodeSize, childX + nodeSize/2, y + 60);
            drawSkillTreeNodeSmart(g, node.right, childX, y + 60, nodeSize, minGap);
        }
        // 画节点
        drawSkillNode(g, node.val, x, y, nodeSize);
    }
    private void drawSkillNode(Graphics2D g, SkillTree.Skill skill, int x, int y, int size) {
        if (skill.unlocked) g.setColor(new java.awt.Color(100, 200, 255));
        else g.setColor(new java.awt.Color(60, 60, 60));
        g.fillRoundRect(x, y, size, size, 20, 20);
        g.setColor(Color.WHITE);
        g.setStroke(new java.awt.BasicStroke(3));
        g.drawRoundRect(x, y, size, size, 20, 20);
        if (skill.iconPath != null) {
            java.awt.Image img = java.awt.Toolkit.getDefaultToolkit().getImage(skill.iconPath);
            g.drawImage(img, x+8, y+8, size-16, size-16, null);
        }
        g.setColor(Color.WHITE);
        g.setFont(new Font("微软雅黑", Font.BOLD, 14));
        java.awt.FontMetrics fm = g.getFontMetrics();
        int textWidth = fm.stringWidth(skill.name);
        g.drawString(skill.name, x + (size-textWidth)/2, y + size + 18);
        if (skill.maxLevel > 1) {
            g.setFont(new Font("Arial", Font.PLAIN, 12));
            g.drawString("Lv " + skill.level + "/" + skill.maxLevel, x+size-38, y+size-8);
        }
    }
} 